#include <stdio.h>
#include <stdlib.h>

#include "MenuLogic.h"
#include "../Graphic/Graphic.h"

enum S_Status { S_Build, S_Ready, S_Error, S_Delete };

/* Menu */
typedef struct {
    int Status;
    char *Name;
    int X;
    int Y;
    void Menu_FuncFull;
    void Menu_CallFuncFull;
    int Style;
} M_Menu;

M_Menu *MenuStack;

int Menu_Build = 0;

void Menu_Init()
{
    int i;

    /* Alle Menüs erstellen */
    MenuStack = malloc(Menu_Length* sizeof(M_Menu));

    for(i = 0; i < Menu_Length; i++)
    {
        MenuStack[i].Status = S_Delete;
    }

    Menu_Build = Menu_Create( 50, 60, "icon_build", &Graphic_BuildIcon, &MenuLogic_Build);
    Menu_SetStyle(Menu_Build, 1);
}

void Menu_SetStyle(int i, int Style) {
    MenuStack[i-1].Style = Style;
}

int Menu_Create(int X, int Y, char *Titel, void vFunc Menu_Parameter, void cFunc Menu_Call_Parameter) {
    int i;

    for(i = 0; i < Menu_Length; i++)
    {
        if(MenuStack[i].Status == S_Delete)
            break;
    }
    if(i >= Menu_Length) {
        logWarning("Kein Platz f\201r noch ein Men\201!");
        return -1;
    }

    /* Wenn er nichts findet -> kein Menü*/
    if( i > Menu_Length)
    {
        logWarning("Cant create the Menu '%s'", Titel);
        return -1;
    }
    MenuStack[i].Status = S_Ready;
    MenuStack[i].Name = Titel;
    MenuStack[i].X = X;
    MenuStack[i].Y = Y;
    MenuStack[i].Menu_Func = vFunc;
    MenuStack[i].Menu_CallFunc = cFunc;
    MenuStack[i].Style = 0;

    return i+1;
}


/* Maus über einen menüeintrag? */
int Menu_MouseOver(int i, int X, int Y)
{
    float Zoom = Menu_Zoom*(32);

    if((MenuStack[i].X-(int)Zoom < X) && (MenuStack[i].X+(int)Zoom > X))
    if((MenuStack[i].Y-(int)Zoom < Y) && (MenuStack[i].Y+(int)Zoom > Y))
        return 1;
    return 0;
}

int Menu_Input(int X, int Y)
{
    int i, n = 0;

    /* Jedes Menü abarbeiten */
    for(i = 0; i < Menu_Length; i++)
    {
        /* Menü in Betrieb? */
        if(MenuStack[i].Status == S_Ready)
        {
            /* Maus über ein Menü eintrag? */
            if(Menu_MouseOver(i, X, Y)) {
                /* Ausführen */
                MenuStack[i].Menu_CallFunc( i, MenuStack[i].Name);
                n++;
            }
        }
    }

    return n;
}

void Menu_Remove(int i)
{
    /* Gelöscht */
    MenuStack[i-1].Status = S_Delete;
}

void Menu_Process()
{
    int i;
    int X,Y;
    int Anzahl = 0;
    float Zoom;

    for(i = 0; i < Menu_Length; i++)
    {
        /* Normalwert setzen */
        Zoom = Menu_Zoom;

        if(MenuStack[i].Status == S_Ready)
        {
            X = MenuStack[i].X;
            Y = MenuStack[i].Y;

            /* Falls darüber "Anzeigen" */
            if(Menu_MouseOver(i, Input.X, Input.Y))
                Zoom = Zoom+0.1;

            /* Falls es ein Style hat -> Größer anzeigen */
            if(MenuStack[i].Style)
                Zoom += 0.2;

            Graphic_Circle( (int)X, (int)Y, Zoom, MenuStack[i].Style);
            MenuStack[i].Menu_Func(X,
                                   Y,
                                   Zoom,
                                   MenuStack[i].Name);
        }
    }
}
